#include <iostream>
#include <cmath>
using namespace std;
int simple(int n)
{
    if(n<=1)
        return 0;
 
     for (int i = 2; i<=sqrt(n); i++)
          if (n % i == 0)
            return 0;
 
     return 1;
}
    int main()
{
    int ch,c1,c2,c3,c4,c5,c6,c7,c8,mc=0;
    cin >>ch;
    if (ch<=0 || (ch>9999 || ch<1000))
        cout<<0;
    else{
        c1=ch/100;
        c2=ch/100%10;
        c3=ch/10%10;
        c4=ch/10%100;
        c5=ch%100;
        c6=ch/10;
        c7=ch%10;
        c8=ch/1000;
        if (simple(ch) == 1)
            mc = ch;
        if ((simple(c1) == 1) && c1>mc)
            mc = c1;
        if ((simple(c2) == 1) && c2>mc)
            mc = c2;
        if ((simple(c3) == 1) && c3>mc)
            mc = c3;
        if ((simple(c4) == 1) && c4>mc)
            mc = c4;
        if ((simple(c5) == 1) && c5>mc)
            mc = c5;
        if ((simple(c6) == 1) && c6>mc)
            mc = c6;
        if ((simple(c7) == 1) && c7>mc)
            mc = c7;
        if ((simple(c8) == 1) && c8>mc)
            mc = c8;
        cout<<mc;
    }
    
}
